import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
    meta: {
      keepAlive: true
    }
  },
  // 用于快速登录
  {
    path: "/login-loading",
    component: () => import("@/views/login/index-loading"),
    hidden: true,
    meta: {
      keepAlive: true
    }
  },
  {
    path: "/",
    redirect: "/choose-company"
  },
  {
    path: "/edit-password",
    component: () => import("@/views/login/edit-password"),
    hidden: true,
    meta: {
      keepAlive: true
    }
  },
  {
    path: "/choose-company",
    component: () => import("@/views/choose-company/index"),
    hidden: true,
    meta: {
      keepAlive: true
    }
  },
  {
    path: "/zh-control-view",
    component: Layout,
    redirect: "noRedirect",
    name: "ZhControlViews",
    // meta: { title: "工作台", icon: "system-settings" },
    children: [
      {
        path: "/zh-control-view",
        name: "ZhControlView",
        component: () => import("@/views/zh-console-view/index"),
        hidden: false
        // meta: {
        //   keepAlive: true
        // }
        // hidden: true,
        // meta: { title: "工作台", icon: "system-settings" }
      }
    ]
  },
  // {
  //   path: "/select-farm",
  //   component: () => import("@/views/choose-company/select-farm"),
  //   hidden: true
  // },
  {
    path: "/register-user",
    component: () => import("@/views/register-user/index"),
    hidden: true,
    meta: {
      keepAlive: true
    }
  },
  {
    path: "/find-password",
    component: () => import("@/views/find-password/index"),
    hidden: true,
    meta: {
      keepAlive: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
    meta: {
      keepAlive: true
    }
  }
];

// 导入全部路由
const files = require.context('./modules', true, /\.js$/)
const modules = []
files.keys().forEach(key => {
  modules.push(files(key).default)
  // console.log(key)
})
// 模块名称有误，直接导入会出错，暂时放置在others文件夹下
// import StatisticalReportRouter from "./modules/digital-operation/StatisticalReportRouter"; // 模块名称重复
// import AgrSafetyEvaluation from "./modules/digital-operation/AgrSafetyEvaluation"; // 模块名称重复
export const asyncRoutes = [
  ...modules
  // {
  //   path: "external-link",
  //   component: Layout,
  //   children: [
  //     {
  //       path: "https://panjiachen.github.io/vue-element-admin-site/#/",
  //       meta: { title: "External Link", icon: "link" }
  //     }
  //   ]
  // },
  // 404 page must be placed at the end !!!
  // { path: "*", redirect: "/404", hidden: true }
];

const createRouter = () =>
  // console.log(constantRoutes)
  new Router({
    // zhtip:使用history模式需要与后端配合，不设置默认为hash模式，hash模式为最常用的模式，就是头部难看点
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
